//
//  EntryViewController.m
//  Wtracker
//
//  Created by Anna on 6/28/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "EntryViewController.h"
#import "SettingsViewController.h"
#import "WtrackerAppDelegate.h"
#import "dateViewController.h"
#import "weightLogViewController.h"
#import "shareViewController.h"

#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

#define UOMTITLE @"Unit of Measure"
#define STARTED @"Started"

@implementation EntryViewController

@synthesize unitsL, myDict, dateButton, theWeightTF;


- (void)viewDidLoad {
	/* get the latest settings */
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate loadMySettings];
	myDict = appDelegate.myDict;	
	[self setupDateButton];
	
	NSLog(@"in Entry view did load, myDict: %@", myDict);
	self.navigationItem.title = @"Weight Entry";
	self.navigationItem.rightBarButtonItem = BARBUTTON(@"Settings", @selector(viewSettings));
	
	NSNumber *startedsetup = [myDict objectForKey:STARTED] ;
	NSLog(@"the started_setup: %d", [startedsetup intValue]);
	if([startedsetup intValue] <= 0){
		[self viewSettings];
	}
	unitsL.text = [myDict objectForKey:UOMTITLE];
	
}

- (void)viewDidAppear:(BOOL)animated{
	
	NSLog(@"in Entry view did appear");
	[self setupDateButton];
	
}

-(IBAction)viewDate{
	dateViewController *dVC = [[dateViewController alloc] initWithNibName:@"dateViewController" bundle:nil];
	[[self navigationController] pushViewController:dVC animated:YES];
	[dVC release];
	
}

-(void)setupDateButton{
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	if(appDelegate.tmpDate.length == 0){

		NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
		[dateFormatter setDateStyle:NSDateFormatterShortStyle];
		[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
		
		NSDate *today = [NSDate date];
		NSString *todayStr = [dateFormatter stringFromDate:today];
		[dateButton setTitle:todayStr forState:UIControlStateNormal];
		NSLog(@"length is zero? todayStr; %@",todayStr);
	} else {
		[dateButton setTitle:appDelegate.tmpDate forState:UIControlStateNormal];
		NSLog(@"length is not zero: %@",appDelegate.tmpDate);
	}
}
-(IBAction)saveLog{
	NSLog(@" in savelog ibaction");

	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	float theWeight = [[theWeightTF text] doubleValue];
	NSString *theDate = dateButton.titleLabel.text;
	
	if(theWeight <= 0){
		NSString *theMessage = @"Please enter a weight.";
		[self showAlert:theMessage];
		return;
	}
	BOOL success = [appDelegate logWeight:theWeight logDate:theDate];
	if(success){
		
		weightLogViewController *wlvc = [[weightLogViewController alloc] initWithNibName:@"weightLogViewController" bundle:nil];
		[[self navigationController] pushViewController:wlvc animated:YES];
		[wlvc release];
	} else {
		NSString *theMessage = @"Error saving weight.";
		[self showAlert:theMessage];
	}
}

-(IBAction)shareInfo{
	shareViewController *svc = [[shareViewController alloc] initWithNibName:@"shareViewController" bundle:nil];
	[[self navigationController] pushViewController:svc animated:YES];
	[svc release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}


-(IBAction)viewSettings{
	SettingsViewController *sVC = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
	[self.navigationController pushViewController:sVC animated:YES];
	[sVC release];
}

-(IBAction)viewWeightLog{
	NSLog(@"in vwl ibaction");
	weightLogViewController *wlvc = [[weightLogViewController alloc] initWithNibName:@"weightLogViewController" bundle:nil];
	[[self navigationController] pushViewController:wlvc animated:YES];
	[wlvc release];
}

-(void)showAlert:(NSString *)theMessage{
	UIAlertView *infoAlert = [[[UIAlertView alloc] initWithTitle:@"Error" message:theMessage delegate:self cancelButtonTitle:@"OK" 
											   otherButtonTitles:nil] autorelease];
	[infoAlert show];

	
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
	[theWeightTF release];
	[dateButton release];
	[myDict release];
	[unitsL release];
    [super dealloc];
}


@end
